<template>
  <div class="">
    <div class="topTitle">
      <h2>开通城市</h2>
    </div>

    <div class="topBtn">
      <el-button type="primary" @click="dialogFormVisible = true"
        >+添加城市</el-button
      >
    </div>
    <el-table :data="CityData" style="width: 100%">
      <el-table-column prop="id" label="省编号" width="800" align="center">
      </el-table-column>
      <el-table-column
        prop="province"
        label="省名字"
        width="800"
        align="center"
      >
      </el-table-column>
    </el-table>

    <!-- AddForm -->
    <el-dialog title="添加城市" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="120px"
        class="demo-ruleForm"
        style="width: 300px; margin-top: 30px"
      >
        <el-form-item label="选择省市" prop="imports">
          <el-select v-model="ruleForm.province" placeholder="请选择省">
            <el-option
              v-for="item in province"
              :key="item.id"
              :label="item.province"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input
            v-model="ruleForm.city"
            placeholder="请输入城市名称"
          ></el-input>
        </el-form-item>
        <div>
          <el-button type="primary" style="margin-left: 50px" @click="addpro"
            >添加</el-button
          >
          <el-button
            style="margin-left: 40px"
            @click="dialogFormVisible = false"
            >取消</el-button
          >
        </div>
      </el-form>
    </el-dialog>

    <!-- 分页 -->
    <div class="block" align="center">
      <el-pagination
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      CityData: [],
      province: [],
      current: 1,
      total: 0,
      pageSize: 10,
      count: 0,
      dialogTableVisible: false,
      dialogFormVisible: false,
      ruleForm: {
        province: "",
      },
      data: {
        limit: this.pageSize,
        page: 1,
      },
    };
  },

  //添加城市
  created() {
    this.$axios({
      url: "/province/findAll",
      method: "get",
      data: { limit: this.count },
    }).then((res) => {
      // console.log(res);

      this.province = [...res.data.data];
      // console.log(this.province);
    });

    //列表
    this.$axios({
      url: "/province/findAll",
      method: "get",
      params: {
        limit: this.pageSize,
        page: this.page,
      },
    }).then((res) => {
      // console.log(res);
      if (res.data.code === 0) {
        this.CityData = res.data.data;
        // console.log(this.total);
        // console.log(res.data.data.length);
        this.total = res.data.data.length;
      }
    });
  },
  methods: {
    //添加
    addpro() {
      this.$axios({
        url: "/province/addCity",
        method: "post",
        data: {
          provinceId: this.ruleForm.province,
          city: this.ruleForm.city,
        },
      }).then((res) => {
        if (res.data.code === 0) {
          alert("添加城市成功");
        }
        // console.log(this.ruleForm.province);
        // console.log(this.ruleForm.city);
      });
      this.dialogFormVisible = false;
    },
    //分页
    handleCurrentChange(val) {
      let data = this.data;
      data.page = val;
      this.pageP(data);
    },
    pageP() {
      this.$axios({
        url: "/province/findAll",
        method: "get",
        data: this.data,
      }).then((res) => {
        if (res.data.code === 0) {
          this.CityData = res.data.data;
          this.total = res.data.count;
        }
      });
    },
  },
};
</script>

<style scoped lang="less">
.topBtn {
  margin-top: 10px;
  margin-left: 1%;
}
.contBox {
  width: 98%;
  margin: 2% auto;
  box-shadow: 0px 3px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  border-radius: 4px;
}
.fenye {
  width: 98%;
}
/deep/.el-pagination {
  margin: 0 auto;
  width: 31%;
}
</style>

